<?php
namespace Home\Controller;
use Think\Controller;

class WxController extends Controller {
	/**
	 * [index 微信事件以及key的回复]
	 * author liuzm
	 * time   2016年7月4日
	 * @return [type] [description]
	 */
	function index(){
		$options=C('options');
		//验证签名		
		$weObj=new \Org\Util\Wechat($options);
		$weObj->valid();
		$weObj->checkAuth();//这里是access_toke
		//获取事件类型
		$type = $weObj->getRev()->getRevType();
		switch ($type) {
			//自定义关键字回复
			case \Org\Util\Wechat::MSGTYPE_TEXT:
				$txt=$weObj->getRev()->getRevContent();
				$list=M('keyword')->where('keyword="'.$txt.'"')->find();
				if (!empty($list)) {
					$weObj->text($list['content'])->reply();
				}else{
					$list_fei=M('keyword')->where('id=2')->find();
					$weObj->text($list_fei['content'])->reply();
				}
				exit;
				break; 

			//关注事件
			case \Org\Util\Wechat::MSGTYPE_EVENT:
				$key = $weObj->getRev()->getRevEvent();//获取接收事件推送
				//获得关注者的openid
				$openid = $weObj->getRev()->getRevFrom();
				//获得微信用户信息
				$userinfo = $weObj->getUserInfo($openid); 
				if ($key['event']==\Org\Util\Wechat::EVENT_SUBSCRIBE) {
					$user=M('user');
					//首先判断是否之前已经关注过
					$arr=$user->where('openid="'.$openid.'"')->find();
					$data=array(
        					"subscribe"=>$userinfo['subscribe'],	
        					"nickname"=>$userinfo['nickname'],
        					"openid"=>$userinfo['openid'],
        					"sex"=>$userinfo['sex'],
        					"city"=>$userinfo['city'],
        					"country"=>$userinfo['country'],
        					"province"=>$userinfo['province'],
        					"language"=>$userinfo['language'],
        					"headimgurl"=>$userinfo['headimgurl'],
        					"subscribe_time"=>date('Y-m-d H:i:s',$userinfo['subscribe_time']),
        					"unionid"=>$userinfo['unionid'],
        					"remark"=>$userinfo['remark'],
        					"groupid"=>$userinfo['groupid'],
        					"ctime"=>time(),
        				);
					//如果openid不为空，则更新用户资料
					if (!empty($arr)) {
						$result=$user->where('openid="'.$openid.'"')->data($data)->save();
					}else{
						$result=$user->data($data)->add();
					}
						//失败返回
					if (false == $result) {
						$weObj->text('插入或更新数据库失败')->reply();
					}else{
						//成功返回
						$arr=M('keyword')->where('id=1')->find();
						$weObj->text($arr['content'])->reply();

					}
					//取消关注事件
				}elseif ($key['event']==\Org\Util\Wechat::EVENT_UNSUBSCRIBE) {
					$user=M('user');
					//首先找到取消关注的这个用户
					$arr=$user->where('openid="'.$openid.'"')->find();
					$data=array(
						"subscribe"=>0,
						);
					$result=$user->where('openid="'.$openid.'"')->data($data)->save();
				}else{
					//查找所有的菜单的key和内容回复相对应的
					$menu=M('menu');
					$list=$menu->join("INNER JOIN `sy_content` on sy_menu.one_key=sy_content.c_menu or sy_menu.two_key=sy_content.c_menu")->where('c_menu="'.$key['key'].'"')->find();
					$type_db=$list['type'];
					//回复纯文本
					if ($type_db=="text") {
						$content=$list['content'];
						$weObj->text($content)->reply();
					}
					//回复图文
					if ($type_db=='pic') {
						$list_pic=$menu->join("INNER JOIN `sy_content` on sy_menu.one_key=sy_content.c_menu or sy_menu.two_key=sy_content.c_menu")->where('c_menu="'.$key['key'].'"')->select();
						$count=count($list_pic);  //数量正确
						$arr=array();
						foreach ($list_pic as $key => $value) {
							$preg ="/<img src=\"(.+?)\".*?>/";//只能有src	
						    preg_match_all($preg,html_entity_decode($value['cover']),$cover);		
							$cover_arr=$cover[1][0];//去除数组中重复的值		
							$arr=array( 
									'Title'=>$value['content'], 
									'Description'=>$value['explain'],
							 		'PicUrl'=>'http://'.$_SERVER['SERVER_NAME'].$cover_arr.'',
									'Url'=>$value['editor'],
								);
							
							$array[$key]=$arr;
					
						}
						$content=$array;
						$weObj->news($content)->reply();
						break;
					}
				}
				break;
			default:
			break;			
		}
	}	
}
?>